﻿using Entity;
using IRepository;
using IService;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Service
{
    public class TargetFormService : ITargetFormService
    {
        private readonly ITargetFormRepository _targetFormRepository;

        public TargetFormService(ITargetFormRepository targetFormRepository)
        {
            _targetFormRepository = targetFormRepository;
        }

        /// <summary>
        /// 获取所有可用的目标表单
        /// </summary>
        public List<TargetForm> GetAll()
        {
            return _targetFormRepository.GetAll();
        }

        /// <summary>
        /// 根据ID获取目标表单（ID有效性校验）
        /// </summary>
        public TargetForm GetById(int target_id)
        {
            if (target_id <= 0)
                throw new ArgumentOutOfRangeException(nameof(target_id), "目标表单ID必须大于0");

            return _targetFormRepository.GetById(target_id);
        }

        /// <summary>
        /// 根据表单名称获取ID（强校验，名称非空时直接返回结果）
        /// </summary>
        public int GetIdByFormName(string target_form)
        {
            if (string.IsNullOrWhiteSpace(target_form))
                throw new ArgumentException("目标表单名称不能为空", nameof(target_form));

            return _targetFormRepository.GetIdByFormName(target_form);
        }

        /// <summary>
        /// 根据名称获取目标表单ID（弱校验，名称为空时返回null）
        /// </summary>
        public int? GetTargetIdByName(string target_form_name)
        {
            if (string.IsNullOrWhiteSpace(target_form_name))
                return null;

            return _targetFormRepository.GetTargetIdByName(target_form_name);
        }

        /// <summary>
        /// 检查目标表单名称是否已存在（自动去除首尾空格，忽略大小写）
        /// </summary>
        public bool ExistsByFormName(string target_form_name)
        {
            if (string.IsNullOrWhiteSpace(target_form_name))
                return false;

            return _targetFormRepository.ExistsByFormName(target_form_name.Trim());
        }
    }
}